package com.wanmait.recruitment.controller;

import com.github.pagehelper.PageInfo;
import com.wanmait.recruitment.pojo.Comment;
import com.wanmait.recruitment.pojo.Job;
import com.wanmait.recruitment.pojo.Userinfo;
import com.wanmait.recruitment.service.CommentService;
import com.wanmait.recruitment.service.ConfigService;
import com.wanmait.recruitment.service.JobService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("manage/comment")
public class CommentListController {
    @Resource
    private CommentService commentService;
    @Resource
    private ConfigService configService;
    @Resource
    private JobService jobService;
    @RequestMapping("list")
    public void list(Model model){
        List<Job> jobs = jobService.findAllJobName();
       model.addAttribute("jobs",jobs);
    }
    @RequestMapping("date")
    public ModelAndView date(String pageNumber,Comment comment,String username,String sex){
        Job job = new Job();
        job.setId(comment.getId());
        Userinfo userinfo = new Userinfo();
        userinfo.setUsername(username);
        userinfo.setSex(sex);
        comment.setJob(job);
        comment.setUserinfo(userinfo);
        ModelAndView modelAndView = new ModelAndView();
        int pageNum;
        try {
            pageNum = Integer.parseInt(pageNumber);
        } catch (Exception e) {
            pageNum = 1;
        }
        int pageSize;
        try {
            pageSize = Integer.parseInt(configService.getValueByKey("comment_config"));
        } catch (Exception e) {
            pageSize = Integer.parseInt(configService.getDefaultValueByKey("comment_config"));
        }
        PageInfo<Comment> commentPageInfo = commentService.findAllPage(comment,pageNum,pageSize);
        List<Comment> comments = commentPageInfo.getList();
        modelAndView.addObject("commentPageInfo",commentPageInfo);
        return modelAndView;
    }
    @RequestMapping("del")
    @ResponseBody
    public void del(String id){
        commentService.delete(Integer.parseInt(id));
    }
    @RequestMapping("batchDel")
    @ResponseBody
    public void batchDel(String idArr){
        commentService.delete(idArr);
    }
    @RequestMapping("reply")
    public ModelAndView reply(String id){
        ModelAndView modelAndView = new ModelAndView();
        List<Comment> comments = null;
        try {
            comments = commentService.selectListByParentId(Integer.parseInt(id));
        } catch (Exception e) {
            modelAndView.setViewName("manage/comment/list");
        }
        modelAndView.addObject("comments",comments);
        return modelAndView;
    }
    @RequestMapping("linkDel")
    @ResponseBody
    public void linkDel(String id){
        commentService.delReply(Integer.parseInt(id));
    }
    @RequestMapping("batchDelReply")
    @ResponseBody
    public void batchDelReply(String ids){
        commentService.batchDelReply(ids);
    }
}
